package cn.tj212.xiaoyin.portal.controller;

import cn.tj212.xiaoyin.common.page.TailPage;
import cn.tj212.xiaoyin.core.consts.domain.ConstsClassify;
import cn.tj212.xiaoyin.core.consts.service.IConstsClassifyService;
import cn.tj212.xiaoyin.core.course.domain.Course;
import cn.tj212.xiaoyin.core.course.service.ICourseService;
import cn.tj212.xiaoyin.portal.business.IPortalBusiness;
import cn.tj212.xiaoyin.portal.vo.ConstsClassifyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 课程分类页面
 */
@Controller
@RequestMapping("/course")
public class CourseListController {

    @Autowired
    private IConstsClassifyService constsClassifyService;

    @Autowired
    private IPortalBusiness portalBusiness;

    @Autowired
    private ICourseService courseService;

    /**
     * 课程分类页
     * @param c
     * @param sort
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(String c, Integer sort, TailPage<Course> page) {
        ModelAndView mv = new ModelAndView("list");
        String curCode = "-1";//当前方向
        String curSubCode = "-2";//当前分类

        //加载所有课程分类
        Map<String, ConstsClassifyVO> classifyMap = portalBusiness.queryAllClassifyMap();
        //所有一级分类
        List<ConstsClassifyVO> classifysList = new ArrayList<>();
        for (ConstsClassifyVO vo : classifyMap.values()
                ) {
            classifysList.add(vo);
        }
        mv.addObject("classifys", classifysList);

        //当前分类
        ConstsClassify curClassify = constsClassifyService.getByCode(c);
        if (null == curClassify) {//没有该分类，加载二级分类
            List<ConstsClassify> subClassifys = new ArrayList<>();
            for (ConstsClassifyVO vo : classifyMap.values()
                    ) {
                subClassifys.addAll(vo.getSubClassifyList());
            }
            mv.addObject("subClassifys", subClassifys);
        } else {
            if (!"0".endsWith(curClassify.getParentCode())){//当前是二级分类
                curSubCode=curClassify.getCode();
                curCode=curClassify.getParentCode();
                mv.addObject("subClassifys",classifyMap.get(curClassify.getParentCode()).getSubClassifyList());//此分类平级的二级分类
            }else {//当前是一级分类
                curCode=curClassify.getCode();
                mv.addObject()
            }
        }
    }
}
